<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['clearsn']){
		$params['clearsn like']='%'.trim($_RQ['clearsn']).'%';
		$return['params']['clearsn']=trim($_RQ['clearsn']);
	}
	
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}else{
		$return['params']['createtime']=array();
	}
	
    $fields=array('id','money','status','order_id','clearsn','createtime');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('core_agent_commission',$params,array($pindex,$psize),$total,$fields,'',$orderby);

	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'佣金管理'),
			array('op'=>'clear','title'=>'批量结算'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'money','title'=>'金额','type'=>'string'),
			array('name'=>'order_id','title'=>'关联订单号','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'clearsn','title'=>'结算单号','type'=>'string'),
			array('name'=>'createtime','title'=>'佣金生成时间','type'=>'time'),
			
		)),
		'search'=>array(
			array('name'=>'createtime','width'=>18,'title'=>'产生时间','type'=>'time_range'),
			array('name'=>'clearsn','width'=>15,'title'=>'结算单号','type'=>'string'),
		)
	);
	$return['statuss']=array(
		'0'=>'待审核',
		'1'=>'待结算',
		'2'=>'已结算',
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='clear'){
	$clear=pdo_getall('core_agent_commission',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));	
	if(empty($clear)){
		exi('没有待结算佣金','error');
	}
	$money=0;
	foreach($clear as $v){
		$money=$v['money']+$money;
	}
	$tid=get_ordersn('core_agent_clear','ordersn');
	$data=array(
		'money'=>$money,
		'createtime'=>TIMESTAMP,
		'ordersn'=>$tid,
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>0
	);
	pdo_instert('core_agent_clear',$data);
	$data1=array(
		'satatus'=>2,
		'clearsn'=>$tid
	);
	pdo_updata('core_agent_clear',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),$data1);
	exi('结算成功','success','/agent.agent_clear/');
}

?>